package org.example.leetCode;

/**
 * @author Administrator
 */
public class IsValidBST {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isValidBST(TreeNode root) {
        return dfs(root);
    }

    private boolean dfs(TreeNode root) {
        if (root.left != null) {
            if (root.val > root.left.val) {
                return dfs(root.left);
            } else {
                return false;
            }
        }
        if (root.right != null) {
            if (root.val < root.right.val) {
                return dfs(root.right);
            } else {
                return false;
            }
        }

        return true;
    }
}
